<?php
namespace app\dlc\common\form;



use app\common\model\Agent;
use app\common\model\CaseItem;
use app\common\model\DeviceAttr;
use app\common\model\Location;
use app\dlc\controller\CommonController;
use think\Db;

class Form
{
    function __construct(){

    }
    static $typeList = [
        1=>'文本框|text',
        2=>'密码框|password',
        3=>'日期|time  ',
        4=>'图片|image',
        5=>'多行文本框|textarea',
        6=>'地图|map',
        7=>'富文本|content',
        8=>'文本框（不可编辑）|readonly',
        9=>'视频|video',
        100=>'下拉框|select',
        103=>'下拉框(可搜索)|select_search',
        101=>'单选框|radio',
        102=>'多选框|multiple',
    ];
    static $requiredList = [
        0=>'选填',
        1=>'必填',
    ];
    static $imageList=[
        1=>'单图',
        2=>'多图'
    ];
    static $depend_conditionList = [
        ">" => ">",
        "=" => "=",
        "!=" => "!=",
        ">=" => ">=",
        "<=" => "<=",
        "<" => "<",
    ];

    static $ruleList = [
        '>:0' => '大于0',
        '>=:0' => '大于等于0',
        'pInt' => '正整数',
        'email' => '邮箱',
        'isMobile' => '手机',
        'number' => '纯数字',
        '>:0|<:100' => '小于100',
        '>:0|<:50' => '小于50',
        '>:0|<=:50' => '小于等于50',
        '>:0|<=:100' => '小于等于100',
        'isArray' => '数组',
    ];
    static $dataList=[
        1=>'数组',
        2=>'表查询'
    ];

    //列表需要的数据
    static $head_botton_array = ['back'=>'返回','add'=>'添加','send'=>'勾选并操作','excelDownload'=>'导出'];
    static $list_botton_array = ['edit'=>'编辑','enable'=>'启用','disable'=>'禁用','del'=>'删除'];
    static $scene_array = [0=>'全部',1=>'添加',2=>'编辑'];
    static function check($data){
        $rule = [];
        foreach ($data as $k=>$v){
            if ($v['rule']){
                if ($v['tig']){     //如果有tig变量就使用这个提示语
                    $rule[$v['key'].'|'.$v['tig']] = $v['rule'];
                }else{
                    $rule[$v['key'].'|'.$v['name']] = $v['rule'];
                }
            }
        }
//        dump($rule);
        (new ValidateTool($rule))->goCheck(2);
    }

   static  function get($_instance){
        $item = new FormItem();
       $res = Db::name('form')->where(['index_name'=>$_instance])->find();
       if ($res){
           $attributeLabels = Db::name('form_item')->where(['form_id'=>$res['id']])->order('sort asc')->column('key,title');
           $form_item = Db::name('form_item')->where(['form_id'=>$res['id']])->order('sort asc')->select();
           $rule = [];
           foreach ($form_item as $key=>$value){
               $type = explode('|',self::$typeList[$value['type']]);
               $type = $type[1];
               $arr = [[$value['key']],'type'=>$type,'required'=>$value['required']];
               if ($value['rule']){
                   $arr['rule'] = $value['rule'];
                   if ($value['rule_tig']) $arr['tig'] = $value['rule_tig'];//验证提示语
               }
               if ($value['depend']){
                   $arr['depend'] = $value['depend'];
               }
               if ($value['type']==4){//图片
                   if ($value['image_type']==2) $arr['more'] = 'true';
               }
               if ($value['data_type']==1){//数组
                   $arr['data'] = json_decode($value['data'],true);
//                    $arr['data'] = $value['data'];
               }
               if ($value['data_type']==2){//表查询
                   $map = [];
                   if ($value['data_map']){
                       $map = json_decode($value['data_map'],true);
                   }
                   $arr['data'] = Db::name($value['data_table'])->where($map)->column($value['data_column']);
               }
               if ($value['depend']){
                   $dependName = Db::name('form_item')->where(['id'=>$value['depend']])->value('key');
                   $arr['depend'] = [$dependName,$value['depend_condition'],(int)$value['depend_value']];
//                    dump($dependName);
               }
               if ($value['linkage']){
                   $arr['linkage'] = json_decode($value['linkage'],true);
               }
                $arr['add_edit'] = $value['add_edit'];
               $rule[] = $arr;
           }
           return $item->loading($attributeLabels,$rule);
       }
        switch ($_instance){
            //-------------------------表单开始--------------------------------------------------------------------------
            case 'form':
                $attributeLabels = [
                    'name' => '表名称',
                    'index_name' => '识别名',
                ];
                $rule = [
                    [['index_name'],'type'=>'text','required'=>true],
                    [['name'],'type'=>'select','required'=>true,'data'=>self::getTablesName()]
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_key':
                $attributeLabels = [
                    'key' => '字段列表',
                ];
                $rule = [
                    [['key'],'type'=>'multiple','required'=>true,'data'=>[]]
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_item':
                $attributeLabels = [
                    'form_id' => 'form表识别名',
                    'key' => '字段名',
                    'title' => '中文名',
                    'sort' => '排序（从小到大）',
                ];
                $rule = [
                    [['form_id'],'type'=>'select','required'=>true,'data'=>Db::name('form')->column('id,index_name')],
                    [['key'],'type'=>'text','required'=>true],
                    [['title'],'type'=>'text','required'=>true],
                    [['sort'],'type'=>'text','required'=>true,'rule'=>'pInt'],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_item_add':
                $attributeLabels = [
                    'key' => '字段名',
                    'title' => '中文名',
                    'sort' => '排序（从小到大）',
                ];
                $rule = [
                    [['key'],'type'=>'select','required'=>true],
                    [['title'],'type'=>'text','required'=>true],
                    [['sort'],'type'=>'text','required'=>true,'rule'=>'pInt'],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_rule':
                $attributeLabels = [
                    'type' => '类型',
                    'required' => '是否必填',
                    'data_type' => '数组类型',
                    'data' => 'json数组',
                    'data_table' => '表查询-表',
                    'data_map' => '表查询-条件json',
                    'data_column' => '表查询-column参数',
                    'image_type' => '图片类型',
                    'linkage' => '联动数组',
                    'rule' => '验证规则',
                    'rule_tig' => '验证提示语(可不填)',
                    'depend' => '依赖关系',
                    'depend_condition' => '依赖条件',
                    'depend_value' => '依赖条件值',
                    'add_edit' => '场景（添加/编辑）',
                ];
                $rule = [
                    [['type'],'type'=>'select','required'=>true,'data'=>self::$typeList],
                    [['required'],'type'=>'select','required'=>true,'data'=>self::$requiredList],
                    [['rule'],'type'=>'select','required'=>false,'data'=>self::$ruleList],
                    [['image_type'],'type'=>'select','required'=>true,'data'=>self::$imageList,'depend'=>['type','=',4]],
                    [['data_type'],'type'=>'select','required'=>true,'data'=>self::$dataList,'depend'=>['type','>=',100]],
                    [['data_table','data_column'],'type'=>'text','required'=>true,'depend'=>['data_type','=',2]],
                    [['data_map'],'type'=>'textarea','required'=>false,'depend'=>['data_type','=',2],'rule'=>'isArray'],
                    [['data'],'type'=>'textarea','required'=>true,'depend'=>['data_type','=',1],'rule'=>'isArray'],
                    [['linkage'],'type'=>'textarea','required'=>false,'depend'=>['type','=',100]],
                    [['rule_tig'],'type'=>'text','required'=>false],
                    [['depend'],'type'=>'select','required'=>false,'data'=>[]],
                    [['depend_condition'],'type'=>'select','required'=>true,'data'=>self::$depend_conditionList,'depend'=>['depend','>',0]],
                    [['depend_value'],'type'=>'text','required'=>true,'depend'=>['depend','>',0]],
                    [['add_edit'],'type'=>'select','required'=>true,'data'=>self::$scene_array],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            //表单结束


            //-------------------列表开始------------------------------------------------------------//
            case 'form_list':
                $attributeLabels = [
                    'table' => '表名称',
                    'parent_id' => '菜单上级',
                    'name' => '菜单名称',
                ];
                $rule = [
                    [['table'],'type'=>'select','required'=>true,'data'=>self::getTablesName()],
                    [['parent_id'],'type'=>'select','required'=>false,'data'=>Db::name('role_oath')->where(['parent_id'=>0])->column('oath_id,name')],
                    [['name'],'type'=>'text','required'=>false],
                ];
                return $item->loading($attributeLabels,$rule);
                break;


            case 'form_list_ajax':
                $attributeLabels = [
                    'table' => '表名称',
                    'head_botton' => '头部按钮',
                    'head_search' => '头部搜索',
                    'list_botton' => '列表按钮',
                    'list_title' => '列表头部标题',
                ];
                $rule = [
                    [['table'],'type'=>'readonly','required'=>false],
                    [['head_botton'],'type'=>'multiple','required'=>false,'data'=>self::$head_botton_array],
                    [['head_search'],'type'=>'multiple','required'=>false,'data'=>[]],//数组在方法里面添加
                    [['list_botton'],'type'=>'multiple','required'=>false,'data'=>self::$list_botton_array],
                    [['list_title'],'type'=>'multiple','required'=>false,'data'=>[]],//数据在方法里面添加
                ];
                return $item->loading($attributeLabels,$rule);
                break;

            case 'form_list_role':
                $attributeLabels = [
                    'role' => '权限',
                ];
                $rule = [
                    [['role'],'type'=>'multiple','required'=>true,'data'=>self::array_key_copy_value(CommonController::$roleList)],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_list_title':
                $attributeLabels = [
                    'title' => '标题',
                ];
                $rule = [
                    [['title'],'type'=>'text','required'=>true],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'form_list_item':
                $attributeLabels = [
                    'type' => '类型',
                    'key' => '字段或者自定义英文名',
                    'name' => '中文名',
                    'url' => '链接',
                    'role' => '权限',
                    'sort' => '排序',
                ];
                $rule = [
                    [['type'],'type'=>'select','required'=>true,'data'=>model('FormListItem')->type],
                    [['key','name','sort'],'type'=>'text','required'=>true],
                    [['url'],'type'=>'text','required'=>false],
                    [['role'],'type'=>'multiple','required'=>false,'data'=>self::array_key_copy_value(CommonController::$roleList)],
                ];
                return $item->loading($attributeLabels,$rule);
                break;


            //-------------------列表结束------------------------------------------------------------//
            case 'website':
                $attributeLabels = [
                    'name' => '网站名称',
                    'url' => '网站链接',
                    'admin_url' => '网站后台链接',
                    'user_name' => '用户名',
                    'password' => '密码',
                ];
                $rule = [
                    [['name','url','admin_url','user_name','password'],'type'=>'text','required'=>true],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'api_type':
                $attributeLabels = [
                    'name' => '接口分类名称',
                    'sort' => '排序（从小到大）',
                    'remark' => '备注',
                ];
                $rule = [
                    [['name'],'type'=>'text','required'=>true],
                    [['sort'],'type'=>'text','required'=>true,'rule'=>'pInt'],
                    [['remark'],'type'=>'text'],

                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'cases':
                $attributeLabels = [
                    'name' => '采集方案名称',
                    'url' => '网站链接',
                ];
                $rule = [
                    [['name','url'],'type'=>'text','required'=>true],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'case_item':
                $attributeLabels = [
                    'name' => '方案名称',
                    'url' => '采集的url',
                    'type' => '采集页:类型',
                    'one_more' => '单链or批量',
                    'cases_id' => '所属采集方案',
					
                ];
                $rule = [
                    [['name','url'],'type'=>'text','required'=>true],
                    [['type'],'type'=>'select','required'=>true,'data'=>CaseItem::$typeList],
                    [['one_more'],'type'=>'select','required'=>true,'data'=>[1=>'单链',2=>'批量']],
                    [['cases_id'],'type'=>'select','required'=>true,'data'=>Db::name('cases')->column('id,name')],
					
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'case_item_rule':
                $attributeLabels = [
                    'type' => '采集类型',
                    'rule' => '规则',
                    'slice_select' => '切片选择器',
                ];
                $rule = [
                    [['type'],'type'=>'select','required'=>true,'data'=>CaseItem::$typeList],
                    [['attr',],'type'=>'text','required'=>true,'depend'=>['type','=',1]],
                    [['rule',],'type'=>'textarea','required'=>true,'depend'=>['type','=',2],'row'=>5,'rule'=>'isArray'],
                    [['slice_select',],'required'=>true,'type'=>'text','depend'=>['type','=',1]],
                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'article':
                $attributeLabels = [
                    'title' => '标题',
                    'keywords' => '关键词',
                    'description' => '描述',
                    'content' => '内容',
                ];
                $rule = [
                    [['title','keywords','description'],'type'=>'text','required'=>true],
                    [['content'],'type'=>'content','required'=>true],

                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'rule_type':
                $attributeLabels = [
                    'name' => '名称',
                    'remark' => '备注',
                ];
                $rule = [
                    [['name'],'type'=>'text','required'=>true],
                    [['remark'],'type'=>'text'],

                ];
                return $item->loading($attributeLabels,$rule);
                break;
            case 'rule':
                $attributeLabels = [
                    'name' => '名称',
                    'rule' => '正则表达式',
                    'rule_type_id' => '所属规则',
                    'sort' => '排序',
                ];
                $rule = [
                    [['name'],'type'=>'text','required'=>true],
                    [['rule'],'type'=>'textarea','required'=>true],
                    [['sort'],'type'=>'text','required'=>true,'rule'=>'pInt'],
                    [['rule_type_id'],'type'=>'select','required'=>true,'data'=>Db::name('rule_type')->column('id,name')],
                ];
                return $item->loading($attributeLabels,$rule);
                break;


        }
    }
    static function array_column(array $array, $column_key, $index_key = null)
    {
        $result = [];
        foreach ($array as $arr) {
            if (!is_array($arr)) continue;

            if (is_null($column_key)) {
                $value = $arr;
            } else {
                $value = $arr[$column_key];
            }

            if (!is_null($index_key)) {
                $key = $arr[$index_key];
                $result[$key] = $value;
            } else {
                $result[] = $value;
            }
        }
        return $result;
    }

    static function array_key_copy_value($arr){
        $data = [];
        foreach ($arr as $key => $item) {
            $data[$item] = $item;
        }
        return $data;
    }
    static function getTablesName(){
        $data = \db()->query('show tables');
        $list = [];
        foreach ($data as $key => $value) {
            foreach ($value as $k => $v) {
                $v = explode('dlc_',$v);
                $v = $v[1];
                $list[$v] = $v;
            }
        }
        return $list;
    }


}